import os
import traceback

from src.bean.model_database_enum import ModelDatabaseEnum
from src.model.model_common_util import print_model_list_entrance
from src.util.common_util import printx, while_input, is_int
from src.util.config_manager import ConfigManager


def delete_model_entrance(config: ConfigManager) -> None:
    print_model_list_entrance(config)
    model_list = config.get_model_list()
    if len(model_list) == 0:
        printx("当前还没训练模型")
        return
    printx("删除模型将 不会 删除他的增量文件，原始数据文件，但会删除模型文件并清空记录")
    mid = while_input("请输入要删除的模型id(-1取消):", is_int)
    if mid == -1:
        return
    filter_models = [item for item in model_list if f'{mid}' == f'{item[ModelDatabaseEnum.id.value]}']
    if len(filter_models) == 0:
        printx(f"未找到id为{mid}的模型")
    else:
        model = filter_models[0]
        try:
            path = model[ModelDatabaseEnum.model_path.value]
            os.remove(path)
        except Exception as e:
            traceback.print_exc()
        config.remove_model(model)
        config.db_save()
        printx("已删除模型")
